package com.example.student.Controller;

import com.example.student.Repository.StudentRepository;
import com.example.student.Service.StudentService;
import com.example.student.entity.ApiResponse;
import com.example.student.entity.Grade;
import com.example.student.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("udents")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("stAll")
    public StudentRepository listAllStudents() {
        List<Student> students = studentService.listAll();
        return (StudentRepository) new ApiResponse(200, "获取学生信息成功", students);
    }

    @GetMapping("/getById/{studentId}")
    public StudentRepository getStudentById(@PathVariable Long studentId) {
        Student student = studentService.getById(studentId);
        return (StudentRepository) new ApiResponse(200, "获取学生信息成功", student);
    }

    @GetMapping("/getGrades/{studentId}")
    public StudentRepository getStudentGrades(@PathVariable Long studentId) {
        List<Grade> grades = studentService.getGrades(studentId);
        return (StudentRepository) new ApiResponse(200, "获取学生成绩成功", grades);
    }

    @PostMapping("/add")
    public StudentRepository addStudent(@RequestBody Student student) {
        studentService.add(student);
        return (StudentRepository) new ApiResponse(201, "添加学生成功", null);
    }
}